<template>
    <div class="collapse-panel">
        <h3>折叠面板</h3>
        <div class="panel">
            <div class="item" v-for="item in filterList" :key="item.id" @click="handleClick(item)">
                <div class="title">
                    <div>{{item.title}}</div>
                    <div>{{activeRef.id===item.id?'收齐':'展开'}}</div>
                </div>
                <div class="content" :ref="`contentRef${item.id}`" :style="contentStyle(item.id)">{{item.content}}</div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data () {
        return {
            activeRef: {
                id: '',
                maxHeight: 0
            }
        }
    },
    computed: {
        filterList () {
            let arr = []
            for (let i = 1; i < 6; i++) {
                let content = '是的冯绍峰水电费水电费手打放大搜嘎打发后刚发合法合法高挥发高挥发个是非得失防守打法所得税法沙发沙发四大四大分'
                if (i % 2 === 0) content = '是的冯绍峰水电费水电费手打放大搜嘎打发后刚发合法合法高挥发高挥发个是非得失防守打法所得税法沙发沙发四大四大分是的冯绍峰水电费水电费手打放大搜嘎打发后刚发合法合法高挥发高挥发个是非得失防守打法所得税法沙发沙发四大四大分'
                let obj = {
                    id: i,
                    title: `标题${i}`,
                    content
                }
                arr.push(obj)
            }
            return arr
        },
        contentStyle () {
            return (id) => {
                let style = {
                    maxHeight: 0
                }
                if (id === this.activeRef.id) style.maxHeight = this.activeRef.maxHeight + 'px'
                return style
            }
        }

    },
    methods: {
        handleClick (item) {
            this.activeRef.id = this.activeRef.id === item.id ? '' : item.id
            this.activeRef.maxHeight = this.$refs[`contentRef${item.id}`][0].scrollHeight
        }

    }
}
</script>

<style lang="less" scoped>
.collapse-panel {
    .panel {
        width: 500px;
        margin: 50px auto;

        .item {
            margin-bottom: 5px;

            .title {
                display: flex;
                justify-content: space-between;
                padding: 5px;
                background: rgb(95, 95, 95);
                color: #fff;
                cursor: pointer;
            }

            .content {
                background: rgb(255, 238, 238);
                max-height: 0;
                overflow: hidden;
                transition: max-height .4s;
            }
        }
    }
}
</style>
